//
//  YMNavigationController.m
//  WeiBo
//
//  Created by youmy on 15/8/18.
//  Copyright (c) 2015年 youmy. All rights reserved.

//  导航栏控制器

#import "YMNavigationController.h"

@interface YMNavigationController ()

@property (nonatomic, strong) UIPanGestureRecognizer *pan;

@end

@implementation YMNavigationController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationBar.translucent = NO;

    self.navigationBar.hidden = YES;

    // 滑动返回
//        self.fd_fullscreenPopGestureRecognizer.enabled = YES;
//        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
//        [self custromNavSetupe];
    self.interactivePopGestureRecognizer.enabled = NO;
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
    }
    return self;
}

+ (void)initialize{
    
    [self setBarButtonItem];
    
    [self setNavigationBar];
    
}

#pragma mark - 设置BarButtonItem

+ (void)setBarButtonItem{
    // 设置整个项目所在item的主题样式
    UIBarButtonItem * item = [UIBarButtonItem appearance];
    
    // 设置普通状态
    NSMutableDictionary * textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    textAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:15];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    // 设置不可用状态
    NSMutableDictionary * disableAttrs = [NSMutableDictionary dictionary];
    disableAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.4];
    
    disableAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:disableAttrs forState:UIControlStateDisabled];
    
    // 返回按钮
    NSString * iponeM = [[UIDevice currentDevice] systemVersion];
    NSArray *sepArr = [iponeM componentsSeparatedByString:@"."];
    //NSLog(@"iponeM:%@",sepArr);
    if ([sepArr[0]integerValue]<9) {
       
        UIOffset offSet;
        offSet.horizontal = -500;
        UIBarButtonItem * backButtonItem = (UIBarButtonItem *)[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil];
        [backButtonItem setBackButtonTitlePositionAdjustment:offSet forBarMetrics:UIBarMetricsDefault];
    }else{
    
        UIBarButtonItem * backButtonItem = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]];
        UIOffset offset;
        offset.horizontal = -500;
        [backButtonItem setBackButtonTitlePositionAdjustment:offset forBarMetrics:UIBarMetricsDefault];
    
    }
    
}

#pragma mark - 设置导航栏

+ (void)setNavigationBar{
    UINavigationBar * navigationBar = [UINavigationBar appearance];
    
    // 设置导航栏颜色
//    navigationBar.barTintColor = [UIColor colorWithHexString:@"#E54747"];
    navigationBar.barTintColor = GREEN_COLOR;
    
    // 设置导航栏标题颜色
    NSMutableDictionary * attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = NAV_TITLE_COLOR;
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    navigationBar.titleTextAttributes = attrs;
    
    navigationBar.tintColor = [UIColor whiteColor];
    
    //去除导航栏底线
    [navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    navigationBar.shadowImage = [[UIImage alloc] init];
}

- (void)custromNavSetupe{
    self.navigationBarHidden = YES;
    // 获取系统自带滑动手势的target对象
//    id target = self.interactivePopGestureRecognizer.delegate;
    // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
//    _pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    // 设置手势代理，拦截手势触发
    _pan.delegate = self;
    // 给导航控制器的view添加全屏滑动手势
    [self.view addGestureRecognizer:_pan];
    // 禁止使用系统自带的滑动手势
    self.interactivePopGestureRecognizer.enabled = NO;
}

- (void)setPopGestureRecognizerEnable:(BOOL)PopGestureRecognizerEnable{
    _PopGestureRecognizerEnable = PopGestureRecognizerEnable;
    if (_PopGestureRecognizerEnable) {
        _pan.delegate = self;
    }else{
        _pan.delegate = nil;
    }
}

// 什么时候调用：每次触发手势之前都会询问下代理，是否触发。
// 作用：拦截手势触发
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    // 注意：只有非根控制器才有滑动返回功能，根控制器没有。
    // 判断导航控制器是否只有一个子控制器，如果只有一个子控制器，肯定是根控制器
    if (self.childViewControllers.count == 1) {
        // 表示用户在根控制器界面，就不需要触发滑动手势，
        return NO;
    }
    return YES;
}

/**
 *  重写这个方法目的：能够拦截所有push进来的控制器
 *
 *  @param viewController 即将push进来的控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.viewControllers.count > 0) { // 这是push进来的控制器不是第一个控制器
        
        /*自动隐藏TabBar*/
        viewController.hidesBottomBarWhenPushed = YES;
        
        // 设置导航栏内容
        
        // 设置左边的返回按钮
        viewController.navigationItem.backBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(back) image:@"nav_button_return" hightImage:@"nav_button_return"];
        
        // 设置右边的更多按钮
        viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(more) image:@"navigationbar_more" hightImage:@"navigationbar_more_highlighted"];
    }
    [super pushViewController:viewController animated:YES];
}


- (void)back{
    // 因为self本来就是一个控制器，self.navigationController这时是nil
    [self popViewControllerAnimated:YES];
}

- (void)more{
    [self popToRootViewControllerAnimated:YES];
}
@end
